﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;
using RN.ERP.BLL;
using RN.ERP.DTO;
using System.Web.Services;
using Newtonsoft.Json;

namespace RN.ERP.Web.Material.Material
{
    public partial class MaterialDetail2 : System.Web.UI.Page
    {
        MaterialBLL bll = new MaterialBLL();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                InitPage();
            }
        }

        private void InitPage()
        {
            hdnId.Value = Request.QueryString["id"];
            BindMPClassData();
        }

        private void BindMPClassData()
        {
            int count = 0;
            List<DTO.MateProperty> properties = bll.GetMaterialProperties(
                0,
                0,
                out count);
            properties.Insert(0, new DTO.MateProperty() { id = 0, cMPName = "--请选择--" });
            ddlMPClass.DataSource = properties;
            ddlMPClass.DataValueField = "id";
            ddlMPClass.DataTextField = "cMPName";
            ddlMPClass.DataBind();
        }

        protected void ddlMPClass_SelectedIndexChanged(object sender, EventArgs e)
        {
            int pid = 0;
            if (int.TryParse(ddlMPClass.SelectedValue, out pid))
            {
                int count = 0;
                List<DTO.MPInfo> properties = bll.GetMPListByPropertyId(
                    pid,
                    0,
                    0,
                    out count);
                rptMPInfo.DataSource = properties;
                rptMPInfo.DataBind();
            }
        }

        protected void lbtnSave_Click(object sender, EventArgs e)
        {

        }

        protected void rptMPInfo_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
            {
                HiddenField hdn = e.Item.FindControl("hdniMPIKind") as HiddenField;
                HiddenField hdn2 = e.Item.FindControl("hdncMPIInfo") as HiddenField;
                TextBox txt1 = e.Item.FindControl("txtSingle") as TextBox;
                TextBox txt2 = e.Item.FindControl("txtMulti") as TextBox;
                DropDownList ddl = e.Item.FindControl("ddlSelect") as DropDownList;
                CheckBoxList cb = e.Item.FindControl("cbSelect") as CheckBoxList;
                if (hdn != null)
                {
                    switch (hdn.Value.Trim())
                    {
                        case "1":
                            ddl.Visible = true;
                            if (!string.IsNullOrEmpty(hdn2.Value))
                            {
                                string[] s = hdn2.Value.Split(new char[] { '!', '#' }, StringSplitOptions.RemoveEmptyEntries);
                                ddl.DataSource = s;
                                ddl.DataBind();
                            }
                            break;
                        case "2":
                            cb.Visible = true;
                            if (!string.IsNullOrEmpty(hdn2.Value))
                            {
                                string[] s = hdn2.Value.Split(new char[] { '!', '#' }, StringSplitOptions.RemoveEmptyEntries);
                                cb.DataSource = s;
                                cb.DataBind();
                            }
                            break;
                        case "3":
                            txt1.Visible = true;
                            break;
                        case "4":
                            txt2.Visible = true;
                            break;
                        default:
                            break;

                    }
                }
            }
        }
    }
}